package com.ruoyi.system.rocketmq;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.RocketMqConstants;
import com.ruoyi.system.domain.DjiAirport;
import com.ruoyi.system.mapper.DjiAirportMapper;
import com.ruoyi.system.service.IDjiAirportService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 泛型可选
 */
@Component
@RocketMQMessageListener(consumerGroup = "group1", topic = RocketMqConstants.AIRPORT_INFO)
public class RocketMQConsumer implements RocketMQListener<String> {
    @Autowired
    DjiAirportMapper djiAirportMapper;
    @Autowired
    IDjiAirportService djiAirportService;



    @Override
    public void onMessage(String message) {
        DjiAirport djiAirport = JSONObject.parseObject(message, DjiAirport.class);

        DjiAirport airport = djiAirportMapper.selectBySn(djiAirport.getSn());
        if (airport == null ){
            djiAirportService.insertDjiAirport(djiAirport);
        }else {

            djiAirport.setId(airport.getId());
            djiAirportService.updateDjiAirport(djiAirport);

        }

        System.out.println("Received message : " + message);
    }
}
